
def cmds_dispatcher():
    # 命令和函数存储的地方
    commands = {}

    # 注册
    def reg(name):
        def _reg(fn):
            commands[name] = fn
            return fn
        return _reg

    def defaultfunc():
        print("Unknown command")

    def dispatcher():
        while 1:
            cmd = input(">>>")
            if cmd.strip() == "quit":
                return
            commands.get(cmd, defaultfunc)()

    return reg, dispatcher

reg, dispatcher = cmds_dispatcher()

@reg("mag")
def foo1():
    print("foo1")

@reg("py")
def foo2():
    print("foo2")

dispatcher()
